﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PMCSerialTransfer.Packets
{
    public class RecvFileNamePacket: APacket
    {
        public override bool Packet(byte[] Data)
        {
            return Process(Data);
        }

        public override bool EndProcess(bool Ok)
        {
            string path;
            SerialSettings settings = new SerialSettings();
            byte[] len = new byte[4];
            for (int i = 0; i < 4; i++)
            {
                if (BitConverter.IsLittleEndian)
                {
                    len[3 - i] = Globals.DataPacket[i];
                }
                else
                {
                    len[i] = Globals.DataPacket[i];
                }
            }
            Globals.FileLen = BitConverter.ToInt32(len, 0);
            Globals.FileName = System.Text.Encoding.Default.GetString(Globals.DataPacket,6,Globals.DataPacket.Length-6);
            Globals.SendMsg[0] = (byte)Globals.ACKType.E_ACK;
            settings.GetSettings();
            path = settings.DefaultFolder + @"\" + Globals.FileName;
            if (File.Exists(@path))
            {
                File.Delete(@path);
            }
            Globals.FileStream = File.Create(@path);
            Globals.FilePtr = 0;
            Globals.Message = "Receiving " + Globals.FileName + " Bytes: " + Globals.FileLen.ToString() + "\n";
            Globals.PacketSize = 0;
           
            Globals.ProgressEvent = Globals.ProgressType.Load;
            return Ok;
        }
    }
}
